package com.lyan.myapplication

import android.os.Bundle
import android.os.PersistableBundle
import android.support.annotation.Nullable
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.Toolbar
import android.text.TextUtils
import android.util.Log
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuItem
import android.widget.FrameLayout
import android.widget.TextView
import android.widget.Toast

abstract class BaseActivity : AppCompatActivity() {
    private var viewContent: FrameLayout? = null
    private var tvTitle: TextView? = null
    private var toolbar: Toolbar? = null
    internal var onClickTopLeftListener: OnClickListener? = null
    internal var onClickTopRightListener: OnClickListener? = null
    internal var menuStr: String = ""
    internal var menuResId: Int = 0


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_base)
        toolbar = findViewById(R.id.toolbar) as Toolbar
        viewContent = findViewById(R.id.viewContent) as FrameLayout
        tvTitle = findViewById(R.id.tv_title) as TextView
        setSupportActionBar(toolbar)
        supportActionBar!!.setDisplayShowTitleEnabled(false)
        supportActionBar!!.setDisplayHomeAsUpEnabled(true)
        supportActionBar!!.setShowHideAnimationEnabled(true)
        LayoutInflater.from(this@BaseActivity).inflate(getContentView(), viewContent)
        init(savedInstanceState)

    }


    protected fun setTitle(title: String) {
        if (!TextUtils.isEmpty(title)) {
            tvTitle!!.text = title
        }
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        Log.d(TAG, "onOptionsItemSelected :" + item.itemId)
        if (item.itemId == android.R.id.home) {
            onClickTopLeftListener?.onClick()
        } else if (item.itemId == R.id.action_settings) {
            onClickTopRightListener?.onClick()

        } else if (item.itemId == R.id.sub_1) {
            Toast.makeText(this@BaseActivity, "点击了sub_1", Toast.LENGTH_SHORT).show()
        } else if (item.itemId == R.id.sub_2) {
            Toast.makeText(this@BaseActivity, "点击了sub_2", Toast.LENGTH_SHORT).show()
        } else if (item.itemId == R.id.sub_3) {
            Toast.makeText(this@BaseActivity, "点击了sub_3", Toast.LENGTH_SHORT).show()
        }
        return true
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        Log.d(TAG, "onCreateOptionsMenu")
        if (menuResId == 0 || TextUtils.isEmpty(menuStr)) {
            menuInflater.inflate(R.menu.menu_base, menu)
        }
        return true
    }

    override fun onPrepareOptionsMenu(menu: Menu): Boolean {
        Log.d(TAG, "onPrepareOptionsMenu")
        if (menuResId != 0) {
            menu.findItem(R.id.action_settings).setIcon(menuResId)
        }
        if (!TextUtils.isEmpty(menuStr)) {
            menu.findItem(R.id.action_settings).title = menuStr
        }
        return super.onPrepareOptionsMenu(menu)
    }

    interface OnClickListener {
        fun onClick()
    }

    protected abstract fun getContentView(): Int
    protected abstract fun init(savedInstanceState: Bundle?)

    protected fun setTopLeftButton(iconResId: Int, onClickListener: OnClickListener) {
        if (iconResId != 0) {
            toolbar!!.setNavigationIcon(iconResId)
        }
        this.onClickTopLeftListener = onClickListener
    }

    protected fun setTopRightButton(menuResId: Int, menuStr: String, onClickListener:OnClickListener) {
        this.menuResId = menuResId
        this.menuStr = menuStr
        this.onClickTopRightListener = onClickListener
    }

    companion object {

        private val TAG = BaseActivity::class.java.name
    }

}
